package com.kma.summer2012.client.view.manager;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.constants.ConstantsPresenter;
import com.kma.summer2012.client.presenters.manager.AddWorkPresenter;
import com.kma.summer2012.client.styles.style2.Recources;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 06.02.13
 * Time: 15:50
 * To change this template use File | Settings | File Templates.
 */

public class AddWorkView extends Composite implements AddWorkPresenter.Display {

    interface AddWorkViewUiBinder extends UiBinder<Widget, AddWorkView> {
    }

    private static AddWorkViewUiBinder ourUiBinder = GWT.create(AddWorkViewUiBinder.class);
    public static ConstantsPresenter constants;
    @UiField
    protected Label pageTitle;

    //Table
    @UiField
    protected TextBox workName;
    @UiField
    protected TextArea workDescription;
    @UiField
    protected TextBox runTime;
    @UiField
    protected ListBox toolsList;
    @UiField
    protected ListBox selectedTools;
    @UiField
    protected ListBox categoryList;

    //Buttons
    @UiField
    protected Button saveButton;
    @UiField
    protected Button cancelButton;


    //getters, setters

    @Override
    public void setPageTitle(String title) {
        this.pageTitle.setText(title);
    }

    @Override
    public String getWorkName() {
        return workName.getValue();
    }

    @Override
    public void setWorkName(String workName) {
        this.workName.setValue(workName);
    }

    @Override
    public String getWorkDescription() {
        return workDescription.getValue();
    }

    @Override
    public void setWorkDescription(String workDescription) {
        this.workDescription.setValue(workDescription);
    }

    @Override
    public String getRunTime() {
        return runTime.getValue();
    }

    @Override
    public void setRunTime(String runTime) {
        this.runTime.setValue(runTime);
    }

    @Override
    public List<String> getSelectedToolsList() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < selectedTools.getItemCount(); ++i) {
            list.add(selectedTools.getValue(i));
        }
        return list;
    }

    @Override
    public void setSelectedToolsList(List<String> list) {
        this.selectedTools.clear();
        for (int i = 0; i < list.size(); ++i) {
            this.selectedTools.addItem(list.get(i));
        }
    }

    @Override
    public void setToolsList(List<String> list) {
        this.toolsList.clear();
        for (int i = 0; i < list.size(); ++i) {
            this.toolsList.addItem(list.get(i));
        }
    }

    @Override
    public void setSelectedParent(int category) {
        this.categoryList.setSelectedIndex(category);
    }

    @Override
    public int getSelectedParent() {
        return this.categoryList.getSelectedIndex();
    }

    @Override
    public void setCategoryList(Map<Integer, String> list) {
        this.categoryList.clear();
        for (Integer i : list.keySet()) {
            this.categoryList.insertItem(list.get(i), i);
        }
    }

    @Override
    public String getSelectedCategory() {
        return categoryList.getValue(categoryList.getSelectedIndex());
    }

    @Override
    public void setCategoryList(List<String> list) {
        this.categoryList.clear();
        for (int i = 0; i < list.size(); ++i) {
            this.selectedTools.addItem(list.get(i));
        }
    }

    @Override
    public HasClickHandlers getSaveButton() {
        return saveButton;
    }

    @Override
    public HasClickHandlers getCancelButton() {
        return cancelButton;
    }

    public Widget asWidget() {
        return this;
    }
    /*
    @Override
    public void setSelectedCategory(String selectedWork) {
        int selectedIndex=-1;

        for (int i = 0; i < categoryList.getItemCount(); i++) {
            String workName=categoryList.getValue(i);
            if(workName.equals(selectedWork)){
                selectedIndex=i;
                break;
            }
        }
        categoryList.setSelectedIndex(selectedIndex);
        assert (selectedIndex!=-1);
    }

    *///////////

    public AddWorkView() {
        HTMLPanel rootPanel = (HTMLPanel) ourUiBinder.createAndBindUi(this);
        constants = GWT.create(ConstantsPresenter.class);

        pageTitle.setText(constants.WORK_ADD());
        pageTitle.setStyleName(Recources.INSTANCE.styles().titleCatalog());
        saveButton.addStyleName(Recources.INSTANCE.styles().Caption());
        cancelButton.addStyleName(Recources.INSTANCE.styles().Caption());
        workName.addStyleName(Recources.INSTANCE.styles().input());
        workDescription.addStyleName(Recources.INSTANCE.styles().textarea());
        runTime.addStyleName(Recources.INSTANCE.styles().input());

        initWidget(rootPanel);
        this.workName.setValue("");
        this.toolsList.setVisibleItemCount(7);
        this.selectedTools.setVisibleItemCount(7);
        this.toolsList.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                int selected = toolsList.getSelectedIndex();
                try {
                    selectedTools.addItem(toolsList.getItemText(selected));
                    toolsList.removeItem(selected);
                } catch (Exception e) {
                }
            }
        });

        this.selectedTools.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                int selected = selectedTools.getSelectedIndex();
                try {
                    toolsList.addItem(selectedTools.getItemText(selected));
                    selectedTools.removeItem(selected);
                } catch (Exception e) {
                }
            }
        });
        this.categoryList.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                int selected = categoryList.getSelectedIndex();
                try {
                    categoryList.setItemSelected(selected, true);
                } catch (Exception e) {
                }
            }
        });
    }
}